/*
 * Copyright (c) 2009 the original author or authors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.as3undohistory.manage
{
	import com.as3undohistory.register.IAccessorRegistration;
	
	import flash.utils.Dictionary;
	
	/**
	 * @inheritDoc 
	 * @author Roland Zwaga
	 * 
	 */
	public class PropertyValueStack implements IPropertyValueStack
	{
		protected var values:Array;
		protected var currentValue:*;
		
		/**
		 * Creates a new instance of PropertyValueStack
		 * @param instance the object to be monitored
		 * @param accessorRegistration IAccessorRegistration implementation which contains the necesary information about which property to monitor
		 * @param initialValue the initial value of the specified property
		 * @see com.as3undohistory.register.IAccessorRegistration IAccessorRegistration
		 * 
		 */
		public function PropertyValueStack(instance:Object, accessorRegistration:IAccessorRegistration, initialValue:*)
		{
			_instance = new Dictionary(true);
			_instance[instance] = null;
			_accessorRegistration = accessorRegistration;
			currentValue = initialValue;
			values = new Array();
		}
		
		private var _instance:Dictionary;
		/**
		 * @inheritDoc 
		 * 
		 */
		public function get instance():Object
		{
			for (var o:Object in _instance) { return o; }9
			return null;
		}
		public function set instance(value:Object):void
		{
			if (value !== instance)
			{
				_instance = new Dictionary(true);
				_instance[value] = null;
			}
		}
		private var _accessorRegistration:IAccessorRegistration;
		/**
		* @inheritDoc 
		*/
		public function get accessorRegistration():IAccessorRegistration
		{
			return _accessorRegistration;
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */
		public function get count():uint
		{
			return values.length;
		}

		/**
		 * @inheritDoc 
		 * 
		 */
		public function pop():*
		{
			currentValue = values.pop();
			return currentValue;
		}

		/**
		 * @inheritDoc 
		 * 
		 */
		public function push(value:*):void
		{
			values.push(currentValue);
			currentValue = value;
		}
		

	}
}